using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Neovolve.Extensibility.VisualStudio.Properties;

namespace Neovolve.Extensibility.VisualStudio
{
    /// <summary>
    /// The <see cref="AddinException"/>
    /// class is used to identify a failure to create menu commands for the addin.
    /// </summary>
    [Serializable]
    public class AddinException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="AddinException"/> class.
        /// </summary>
        public AddinException()
        {
        }

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="AddinException"/> class.
        /// </summary>
        /// <param name="reason">The reason.</param>
        public AddinException(String reason)
        {
            Reason = reason;
        }

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="AddinException"/> class.
        /// </summary>
        /// <param name="reason">The reason.</param>
        /// <param name="message">The message.</param>
        /// <param name="innerException">The inner exception.</param>
        public AddinException(String reason, String message, Exception innerException)
            : base(message, innerException)
        {
            Reason = reason;
        }

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="AddinException"/> class.
        /// </summary>
        /// <param name="reason">The reason.</param>
        /// <param name="message">The message.</param>
        public AddinException(String reason, String message)
            : base(message)
        {
            Reason = reason;
        }

        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="AddinException"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"></see> is zero (0). </exception>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is null. </exception>
        protected AddinException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            if (info != null)
            {
                Reason = info.GetString(Resources.SerializationReason);
            }
        }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is a null reference (Nothing in Visual Basic). </exception>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/></PermissionSet>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(Resources.SerializationReason, Reason);

            base.GetObjectData(info, context);
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>The error message that explains the reason for the exception, or an empty string("").</returns>
        public override string Message
        {
            get
            {
                // Check if a message has been defined
                if (String.IsNullOrEmpty(base.Message) == false)
                {
                    return base.Message;
                }

                return Resources.AddinFailureMessage;
            }
        }

        /// <summary>
        /// Gets or sets the reason.
        /// </summary>
        /// <value>The reason.</value>
        public String Reason
        {
            get;
            set;
        }
    }
}